package com.imooc.ecommerce.filter;

import com.imooc.ecommerce.constant.GatewayConstant;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * <p>HTTP 请求头部携带 Token 验证过滤器, 该过滤器为局部过滤器
 *
 * @author Yaxi.Zhang
 * @since 2022/11/5 10:28
 */
public class HeaderTokenGatewayFilter implements GatewayFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 从 HTTP Header 中寻找 key 为 token, value 为 imooc 的键值对
        String name = exchange.getRequest().getHeaders().getFirst(GatewayConstant.GATEWAY_HEADER_NAME);
        if (Objects.equals(name, GatewayConstant.GATEWAY_HEADER_VALUE)) {
            return chain.filter(exchange);
        }
        // 标记此次请求没有权限, 并结束这次请求
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return HIGHEST_PRECEDENCE + 2;
    }
}
